﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using NPOI.XWPF.UserModel;

namespace FillWordDataByTemplate
{
    class Class3
    {
        /// <summary>
        /// Word模板路径
        /// </summary>
        private string file_template = @"E:\Users\Documents\Visual Studio 2013\Projects\用NPOI读写Word\FillWordDataByTemplate\_files_\这是模板3.docx";

        string file_new =
                @"E:\Users\Documents\Visual Studio 2013\Projects\用NPOI读写Word\FillWordDataByTemplate\_files_\这是新文件" +
                DateTime.Now.ToString("yyyyMMdd_HHmmss") +
                ".docx";


        public MemoryStream Export()
        {

            string filepath = file_template;
            using (FileStream stream = File.OpenRead(filepath))
            {
                XWPFDocument doc = new XWPFDocument(stream);
                //遍历段落
                foreach (var para in doc.Paragraphs)
                {
                    ReplaceKey(para);
                }
                //遍历表格
                var tables = doc.Tables;
                foreach (var table in tables)
                {
                    foreach (var row in table.Rows)
                    {
                        foreach (var cell in row.GetTableCells())
                        {
                            foreach (var para in cell.Paragraphs)
                            {
                                ReplaceKey(para);
                            }
                        }
                    }
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    doc.Write(ms);
                    return ms;
                }
            }

        }
        private void ReplaceKey(XWPFParagraph para)
        {
            //BLL.XmxxBLL XmxxBLL = new BLL.XmxxBLL();
            //Model.Xmxx model = new Model.Xmxx();
            //model = XmxxBLL.GetModel(20);

            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("$电话$", "13423433034");
            dict.Add("$罚款$", "999.89");
            dict.Add("$交钱$", "168.68");


            string text = para.ParagraphText;
            var runs = para.Runs;
            string styleid = para.Style;
            for (int i = 0; i < runs.Count; i++)
            {
                var run = runs[i];
                text = run.ToString();
                //Type t = model.GetType();
                //PropertyInfo[] pi = t.GetProperties();
                
                //foreach (PropertyInfo p in pi)
                //{
                //    if (text.Contains("{$xmxx." + p.Name + "}"))
                //    {
                //        text = text.Replace("{$xmxx." + p.Name + "}", TM.Common.StringHelper.ToString(p.GetValue(model, null)));
                //    }
                //}
                foreach (KeyValuePair<string, string> kv in dict)
                {
                    if (text.Contains(kv.Key))
                    {
                        text = text.Replace(kv.Key, kv.Value);
                    }
                }

                runs[i].SetText(text, 0);
            }
        }
        protected void Button1_Click()
        {
            using (MemoryStream ms = Export())
            {
                //Response.ContentType = "application/vnd.ms-word";
                //Response.ContentEncoding = Encoding.UTF8;
                //Response.Charset = "";
                //Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("123.doc", Encoding.UTF8));
                //Response.BinaryWrite(Export().GetBuffer());
                //Response.End();

                //StreamReader reader = new StreamReader(ms);
                //ms.Position = 0;
                //FileInfo f = new FileInfo(file_new);
                //FileStream fs = f.Create();
                //ms.CopyTo(fs);
                //ms.Position = 0;
                //var text = reader.ReadToEnd();
                //Console.WriteLine(text);


                XWPFDocument doc = new XWPFDocument(); 
                doc.Write(ms);
            }
        }

    }
}
